using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MiddlewareServer;
using System.Net;

namespace Server
{
    class Server : IServer_rpt
    {
        IServer_cmd mServer;

        DataTable dtClientsTable = new DataTable("ClientsTable");
        DataColumn dcCID = new DataColumn("CID", typeof(int));
        DataColumn dcClientShips = new DataColumn("ClientShips", typeof(DataTable));

        public Server()
        {
            Console.WriteLine("[S]::CONSTRUCTOR");
            dtClientsTable.Columns.Add(dcCID);
            dtClientsTable.Columns.Add(dcClientShips);
            mServer = new MiddlewareServer.MiddlewareServer(this);
        }

        public void ready(uint a_uiClientID, DataTable a_dtShipsTable)
        {
            Console.WriteLine("[S]::READY");
            if ((a_uiClientID >= 0) && (0 != a_dtShipsTable.Rows.Count))
            {
                DataRow drNewClient = dtClientsTable.NewRow();

                drNewClient[dcCID] = a_uiClientID;
                drNewClient[dcClientShips] = a_dtShipsTable;
                dtClientsTable.Rows.Add(drNewClient);

            }
            else
            {
                mServer.readyResult(a_uiClientID, MiddlewareServer.eReturnErrorCode.Incorect_data);
            }
        }

        public void shoot(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos)
        {
            Console.WriteLine("[S]::SHOOT");
            
            if (a_uiClientID >= 0)
            {
                if ((10 > a_uiXPos) && (0 <= a_uiXPos) && (10 > a_uiYPos) && (0 <= a_uiYPos))
                {
                    uint uiShooted = 0;

                    //DataRow[] RowResult = dtClientsTable.Select(string.Format("{0} != {1)", "CID", a_uiClientID));

                    uint uiCount;
                    //for(uiCount = 0; uiCount =< RowResult.Length; uiCount++)
                    //{
                    //    if( RowResult[uiCount][dcClientShips] = ;
                    //}

                    //MiddleWare.shootPerformed(a_uiClientID, a_uiXPos, a_uiYPos, uiShooted);
                    uiShooted = 5; //fake
                    mServer.shootResult(a_uiClientID, a_uiXPos, a_uiYPos, uiShooted);
                }
                else
                {
                    //MiddleWare.shootPerformed(a_uiClientID, Incorect_data);
                }
            }
            else
            {
                //MiddleWare.shootPerformed(a_uiClientID, Incorect_data);
            }
            
            return;
        }

        public void connect(IPAddress ip)
        {
            Console.WriteLine("[S]::CONNECT");
            mServer.connectResult(MiddlewareServer.eReturnErrorCode.Success);
        }

        #region IServer_rpt Members


        public void disconnect(IPAddress ip)
        {
            Console.WriteLine("[S]::DISCONNECT");
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

    /*
    enum eReturnErrorCode
    {
        Success = 0,
        Timeout,
        Client_not_available,
        Server_not_available,
        Incorect_data
    }

    public interface IServer_rpt
    {
        void ready(uint a_uiClientID, DataTable a_dtShipsTable);
        void shoot(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos);
        //void disconnect();
        //void connect();
    }

    //public interface IServer_cmd
    //{
    //    void readyResult(uint a_uiClientID, eReturnErrorCode a_eRetCode);
    //    void shootPerformed(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos);
    //    //void disconnect();
    //    //void connect();
    //}
     * */
}
/*
enum eReturnErrorCode
{
    Success = 0,
    Timeout,
    Client_not_available,
    Server_not_available,
    Incorect_data
}

public interface IClientServer_rpt
{
    void connect(uint a_uiClientID);
    void ready(uint a_uiClientID, DataTable a_dtShipsTable);
    void shoot(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos);
    void disconnect(uint a_uiClientID);
}

//public interface IClientServer_cmd
//{
//    void readyResult(uint a_uiClientID, eReturnErrorCode a_eRetCode);
//    void shootPerformed(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos);
//    void disconnect(uint a_uiClientID);
//    void connect(eReturnErrorCode a_eRetCode);
//}
*/